package com.hexanet.client.writer;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

import flexjson.JSONSerializer;
import flexjson.transformer.NullTransformer;

public class HexanetWriter {

    public HttpPut getPutRequest(String path, DefaultHttpClient client,
	    Object object) {
	HttpPut putRequest = new HttpPut(path);

	HttpParams params = client.getParams();
	params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
		HttpVersion.HTTP_1_1);
	params.setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(15000));
	params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
		new Integer(15000));

	JSONSerializer serializer = new JSONSerializer();
	StringEntity entity = null;
	try {
	    entity = new StringEntity(serializer.serialize(object));
	    entity.setContentType("application/json");
	} catch (UnsupportedEncodingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

	putRequest.setEntity(entity);

	return putRequest;

    }

    public HttpPost getPostRequest(String path, DefaultHttpClient client,
	    Object object) {
	HttpPost postRequest = new HttpPost(path);

	HttpParams params = client.getParams();
	params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
		HttpVersion.HTTP_1_1);
	params.setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(15000));
	params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
		new Integer(15000));

	JSONSerializer serializer = new JSONSerializer();
	StringEntity entity = null;
	try {
	    entity = new StringEntity(serializer.serialize(object));
	    entity.setContentType("application/json");
	} catch (UnsupportedEncodingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

	postRequest.setEntity(entity);

	return postRequest;

    }

}
